#include "dlgcustom.h"
//(*InternalHeaders(MyFtpDialog)
#include <wx/xrc/xmlres.h>
#include <wx/stdpaths.h>
#include <wx/fileconf.h>
#include <configmanager.h>
#include <globals.h>
#include <wx/checkbox.h>
#include <macrosmanager.h>
//*)

//(*IdInit(MyFtpDialog)
//*)

BEGIN_EVENT_TABLE(MyFtpDialog,wxDialog)
    EVT_BUTTON(XRCID("btnBrowse1"), MyFtpDialog::OnBrowse1)
    EVT_BUTTON(XRCID("btnBrowse2"), MyFtpDialog::OnBrowse2)
    EVT_BUTTON(XRCID("btnBrowse3"), MyFtpDialog::OnBrowse3)
	//(*EventTable(MyFtpDialog)
	//*)
END_EVENT_TABLE()

void MyFtpDialog::OnBrowse1(cb_unused wxCommandEvent &event)
{
    const wxString txtSearchPath = XRCCTRL(*this, "upload_so1", wxTextCtrl)->GetValue();
    const wxString dir = ChooseDirectory(nullptr, _("Select so path"), txtSearchPath);
    if (!dir.IsEmpty())
        XRCCTRL(*this, "upload_so1", wxTextCtrl)->SetValue(dir);


}
void MyFtpDialog::OnBrowse2(cb_unused wxCommandEvent &event)
{
    wxString oldPath = XRCCTRL(*this, "upload_so2", wxTextCtrl)->GetValue();
    Manager::Get()->GetMacrosManager()->ReplaceEnvVars(oldPath);
    wxFileDialog dlg(this, _("Select upload so file"), wxEmptyString, oldPath,
                     wxFileSelectorDefaultWildcardStr, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
    PlaceWindow(&dlg);
    if (dlg.ShowModal() == wxID_OK)
    {
        wxString newPath = dlg.GetPath();
        XRCCTRL(*this, "upload_so2", wxTextCtrl)->ChangeValue(newPath);
    }
}

void MyFtpDialog::OnBrowse3(cb_unused wxCommandEvent &event)
{
    wxString oldPath = XRCCTRL(*this, "upload_so3", wxTextCtrl)->GetValue();
    Manager::Get()->GetMacrosManager()->ReplaceEnvVars(oldPath);
    wxFileDialog dlg(this, _("Select upload so file"), wxEmptyString, oldPath,
                     wxFileSelectorDefaultWildcardStr, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
    PlaceWindow(&dlg);
    if (dlg.ShowModal() == wxID_OK)
    {
        wxString newPath = dlg.GetPath();
        XRCCTRL(*this, "upload_so3", wxTextCtrl)->ChangeValue(newPath);
    }
}

MyFtpDialog::MyFtpDialog(wxWindow* parent)
{
	//(*Initialize(MyFtpDialog)
	wxXmlResource::Get()->LoadObject(this,parent,_T("MyFtpDialog"),_T("wxDialog"));
    // 持久化
    //GET PATH
    wxStandardPathsBase& stdp = wxStandardPaths::Get();
    wxFileName exeFile(stdp.GetExecutablePath());
    wxString m_CfgFilenameStr = exeFile.GetPath(wxPATH_GET_VOLUME|wxPATH_GET_SEPARATOR) + _("Sinfo.ini");
    //OPEN FILE
    wxFileConfig cfgFile(wxEmptyString,     // appname
                         wxEmptyString,      // vendor
                         m_CfgFilenameStr,   // local filename
                         wxEmptyString,      // global file
                         wxCONFIG_USE_RELATIVE_PATH);
    //READ
    wxString ip;
   // wxString user;
    //wxString password;
    //wxString port = _T("6666") ;
    //wxString line ;
   // wxString upload_file ;
   // bool upload=false ;
    wxString upload_so1 ;
    wxString upload_so2 ;
    wxString upload_so3 ;
  //  wxString command1 ;
  //  wxString command2 ;
    cfgFile.Read(_("IP"),  &ip ) ;
    // cfgFile.Read(_("USER"),  &user ) ;
    //cfgFile.Read(_("PASSWORD"),  &password ) ;
    //cfgFile.Read(_("PORT"), &port ) ;
    //cfgFile.Read(_("LINE"), &line ) ;
    //cfgFile.Read(_("UPLOAD_FILE"), &upload_file ) ;
    //cfgFile.Read(_("UPLOAD"), &upload ) ;
    cfgFile.Read(_("UPLOAD_SO1"), &upload_so1 ) ;
    cfgFile.Read(_("UPLOAD_SO2"), &upload_so2) ;
    cfgFile.Read(_("UPLOAD_SO3"), &upload_so3 ) ;
    //cfgFile.Read(_("COMMAND1"), &command1) ;
    //cfgFile.Read(_("COMMAND2"), &command2) ;

    XRCCTRL(*this, "server_ip",      wxTextCtrl)->SetValue(ip);
   // XRCCTRL(*this, "upload", wxCheckBox)->SetValue(!!upload);
    //XRCCTRL(*this, "server_user",      wxTextCtrl)->SetValue(user);
    //XRCCTRL(*this, "server_password",      wxTextCtrl)->SetValue(password);
    //XRCCTRL(*this, "break_point_line",      wxTextCtrl)->SetValue(line);
    //XRCCTRL(*this, "upload_file",      wxTextCtrl)->SetValue(upload_file);
    XRCCTRL(*this, "upload_so1",      wxTextCtrl)->SetValue(upload_so1);
    XRCCTRL(*this, "upload_so2",      wxTextCtrl)->SetValue(upload_so2);
    XRCCTRL(*this, "upload_so3",      wxTextCtrl)->SetValue(upload_so3);
    //XRCCTRL(*this, "gdb_command1",      wxTextCtrl)->SetValue(command1);
    //XRCCTRL(*this, "gdb_command2",      wxTextCtrl)->SetValue(command2);

    Connect(XRCID("wxID_OK"),wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&MyFtpDialog::OnButton1Click);
	//*)
}

MyFtpDialog::~MyFtpDialog()
{
	//(*Destroy(MyFtpDialog)
	//*)
}


void MyFtpDialog::OnButton1Click(wxCommandEvent& event)
{

   wxString server_ip = XRCCTRL(*this, "server_ip", wxTextCtrl)->GetValue();
//   wxString server_port = XRCCTRL(*this, "server_port", wxTextCtrl)->GetValue();
//   wxString server_user = XRCCTRL(*this, "server_user", wxTextCtrl)->GetValue();
//   wxString server_password = XRCCTRL(*this, "server_password", wxTextCtrl)->GetValue();
//   wxString break_point_line = XRCCTRL(*this, "break_point_line", wxTextCtrl)->GetValue();
   //wxString upload_file = XRCCTRL(*this, "upload_file", wxTextCtrl)->GetValue();
  // bool upload = XRCCTRL(*this, "upload", wxCheckBox)->IsChecked();
   wxString upload_so1 = XRCCTRL(*this, "upload_so1", wxTextCtrl)->GetValue();
   wxString upload_so2 = XRCCTRL(*this, "upload_so2", wxTextCtrl)->GetValue();
   wxString upload_so3 = XRCCTRL(*this, "upload_so3", wxTextCtrl)->GetValue();
  // wxString command1 = XRCCTRL(*this, "gdb_command1", wxTextCtrl)->GetValue();
  // wxString command2 = XRCCTRL(*this, "gdb_command1", wxTextCtrl)->GetValue();
   // m_config.Write(wxT("ftp_ip"),       XRCCTRL(*this, "ftp_ip", wxTextCtrl)->GetValue());
   // wxLogError(server_ip);
    wxStandardPathsBase& stdp = wxStandardPaths::Get();
    wxFileName exeFile(stdp.GetExecutablePath());
    wxString m_CfgFilenameStr = exeFile.GetPath(wxPATH_GET_VOLUME|wxPATH_GET_SEPARATOR) + _("Sinfo.ini");
    //OPEN FILE
    wxFileConfig cfgFile(wxEmptyString,     // appname
                         wxEmptyString,      // vendor
                         m_CfgFilenameStr,   // local filename
                         wxEmptyString,      // global file
                         wxCONFIG_USE_RELATIVE_PATH);

    //WRITE
    cfgFile.Write(_("IP"),  server_ip ) ;
//    cfgFile.Write(_("USER"),  server_user ) ;
//    cfgFile.Write(_("PASSWORD"),  server_password) ;
//    cfgFile.Write(_("PORT"), server_port ) ;
//    cfgFile.Write(_("LINE"), break_point_line ) ;
 //   cfgFile.Write(_("UPLOAD_FILE"), upload_file ) ;
//    cfgFile.Write(_("UPLOAD"), upload ) ;
    cfgFile.Write(_("UPLOAD_SO1"), upload_so1 ) ;
    cfgFile.Write(_("UPLOAD_SO2"), upload_so2 ) ;
    cfgFile.Write(_("UPLOAD_SO3"), upload_so3 ) ;
//    cfgFile.Write(_("COMMAND1"), command1);
//    cfgFile.Write(_("COMMAND2"), command2);
    Destroy();
}
